<?php

namespace App\Http\Controllers\Frontend\Demand;

use App\Http\Controllers\Controller;
use App\Http\Repositories\Demand\DemandRepo;
use App\Http\Requests\Validators\Backend\Demand\DemandValidator;
use App\Traits\HelpFunctions;
use Illuminate\Http\Request;

/**
 * 需求相关
 */
class DemandController extends Controller
{
    use HelpFunctions;

    const VALIDATOR = DemandValidator::class;

    public function __construct()
    {
        $this->self_repo = new DemandRepo;
    }

    public function show($id)
    {
        $demand = $this->self_repo->getOneById($id);

        if (empty($demand)) {
            abort(422, "该需求不存在");
        } else {
            if ($demand["status"] != config("project.common_status.agree")) abort(422, "该需求不存在");
        }
        if (!empty($demand["deadline"]) && $this->compareTime(date("Y-m-d H:i:s"), $demand["deadline"])) abort(422, "该需求已过期");

        return $this->returnResponse($demand);
    }

    /**
     * 用户个人空间查看详情
     */
    public function userShow($id)
    {
        $demand = $this->self_repo->getOneById($id);

        if (empty($demand)) {
            abort(422, "该需求不存在");
        } else {
            if (
                ($demand["status"] != config("project.common_status.agree"))
                &&
                ($this->getUserId() != $demand["user_id"])
            ) abort(422, "该需求不存在");
        }
        if (
            ($this->getUserId() != $demand["user_id"])
            &&
            !empty($demand["deadline"]) && $this->compareTime(date("Y-m-d H:i:s"), $demand["deadline"])
        ) abort(422, "该需求已过期");

        return $this->returnResponse($demand);
    }

    public function search(Request $request)
    {
        //参数验证
        $args = $request->only(
            'title',
            'province_id',
            'city_id',
            'area_id',
            'begin_budget',
            'end_budget',
            'introduction',
            'negotiate',
            'begin_deadline',
            'end_deadline',
            'sn',
            'user_id',
            'begin_time',
            'end_time',
            'type_ids',
        );

        $this->my_validate($args, 'searchRules');

        return $this->self_repo->search($args);
    }

    public function store(Request $request)
    {
        //参数验证
        $args = $request->only(
            'title',
            'province_id',
            'city_id',
            'area_id',
            'budget',
            'introduction',
            'negotiate',
            'deadline'
        );

        $this->my_validate($args, 'storeRules');

        $args['user_id'] = $this->getUserId();
        $args['created_at'] = date('Y-m-d H:i:s');
        $args['sn'] = $this->uSN('DM', $args['created_at']);

        //添加数据
        return $this->returnResponse($this->self_repo->createOne($args));
    }

    public function delete($id)
    {
        return $this->self_repo->deleteById($id);
    }

    public function edit(Request $request, $id)
    {
        //参数验证
        $args = $request->only(
            'title',
            'province_id',
            'city_id',
            'area_id',
            'budget',
            'introduction',
            'negotiate',
            'deadline',
            'status'
        );
        $this->my_validate($args, 'editRules');

        //根据id修改数据
        return $this->returnResponse($this->self_repo->editById($id, $args));
    }

    public function addViews($id)
    {
        $this->self_repo->fieldAddOne([['id', '=', $id]], 'views');

        return true;
    }
}
